<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
    <title>社交平台</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<jsp:include page="/views/header.jsp"/>

<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card mb-4">
                <div class="card-body">
                    <div class="post-header d-flex align-items-center mb-3">
                        <img src="${post.user.avatar != null ? post.user.avatar : 'images/default-avatar.png'}"
                             class="rounded-circle avatar-md me-3" alt="作者头像">
                        <div>
                            <h5 class="mb-0">${post.user.nickname}</h5>
                            <small class="text-muted">${post.createdAt}</small>
                            <c:if test="${not empty post.location}">
                                <small class="text-muted"> · ${post.location}</small>
                            </c:if>
                        </div>
                    </div>
                    <div class="post-content mb-3">
                        <p class="lead">${post.content}</p>
                    </div>
                    <div class="post-actions d-flex justify-content-between align-items-center">
                        <div>
                            <span class="me-3">
                                <i class="bi bi-hand-thumbs-up"></i> ${post.likeCount} 赞
                            </span>
                            <span>
                                <i class="bi bi-chat"></i> ${post.commentCount} 评论
                            </span>
                        </div>
                        <div>
                            <c:if test="${post.userId == currentUser.userId}">
                                <a href="${pageContext.request.contextPath}/post/edit?id=${post.postId}"
                                   class="btn btn-sm btn-outline-secondary me-2">编辑</a>
                                <form action="${pageContext.request.contextPath}/post/delete" method="post" class="d-inline">
                                    <input type="hidden" name="postId" value="${post.postId}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger">删除</button>
                                </form>
                            </c:if>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card mb-4">
                <div class="card-header">
                    <h5>评论</h5>
                </div>
                <div class="card-body">
                    <form action="${pageContext.request.contextPath}/comment/add" method="post" class="mb-4">
                        <input type="hidden" name="postId" value="${post.postId}">
                        <div class="mb-3">
                            <textarea class="form-control" name="content" rows="2" placeholder="写下你的评论..." required></textarea>
                        </div>
                        <div class="d-flex justify-content-end">
                            <button type="submit" class="btn btn-primary">提交评论</button>
                        </div>
                    </form>

                    <c:if test="${empty post.comments}">
                        <div class="alert alert-info">暂无评论</div>
                    </c:if>

                    <div class="comment-list">
                        <c:forEach items="${post.comments}" var="comment">
                            <div class="comment mb-3">
                                <div class="d-flex">
                                    <img src="${comment.user.avatar != null ? comment.user.avatar : 'images/default-avatar.png'}"
                                         class="rounded-circle avatar-sm me-3" alt="评论者头像">
                                    <div class="flex-grow-1">
                                        <div class="d-flex justify-content-between">
                                            <h6 class="mb-1">${comment.user.nickname}</h6>
                                            <small class="text-muted">${comment.createdAt}</small>
                                        </div>
                                        <p class="mb-1">${comment.content}</p>
                                    </div>
                                </div>
                            </div>
                        </c:forEach>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<jsp:include page="/views/footer.jsp"/>